package com.arsn.myf.common.util.date;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期处理工具类
 * @author zhongrj
 * @since 2021-11-1
 */
public class DateUtil {

    /**
     * 获取当前时间
     * @return
     */
    public static String getTime(){
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    /**
     * 获取本周及之前4周的集合
     * @return
     */
    public static List<Map<String,Object>> getWeekDate(){
        //获取本周，及之前4周的日期
        Calendar calendar = Calendar.getInstance();
        //设置星期一为一周开始的第一天
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于今年的第几周
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
//        int weekOfYear = 1;
        List<Map<String,Object>> list = new ArrayList<>();
        //遍历
        for (int i = 4; i >-1 ; i--) {
            Map<String, Object> map0 = new HashMap<>();
            if ((weekOfYear-i)==0){
                map0.put("time",52);
                map0.put("value","52周");
            }
            if ((weekOfYear-i)==-1){
                map0.put("time",51);
                map0.put("value","51周");
            }
            if ((weekOfYear-i)==-2){
                map0.put("time",50);
                map0.put("value","50周");
            }
            if ((weekOfYear-i)==-3){
                map0.put("time",49);
                map0.put("value","49周");
            }
            if ((weekOfYear-i)>0){
                map0.put("time",weekOfYear-i);
                map0.put("value",weekOfYear-i+"周");
            }
            //封装
            list.add(map0);
        }
        //返回
        return list;
    }

    /**
     * 获取本月及之前4个月的集合
     * @return
     */
    public static List<Map<String, Object>> getMonthDate() {
        //获取本月，及之前4月的日期
        Calendar calendar = Calendar.getInstance();
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于今年的第几月
        int monthOfYear = calendar.get(Calendar.MONTH)+1;
//        int monthOfYear = 1;
        List<Map<String,Object>> list = new ArrayList<>();
        //遍历
        for (int i = 4; i >-1 ; i--) {
            Map<String, Object> map0 = new HashMap<>();
            if ((monthOfYear-i)==0){
                map0.put("time",12);
                map0.put("value","12月");
            }
            if ((monthOfYear-i)==-1){
                map0.put("time",11);
                map0.put("value","11月");
            }
            if ((monthOfYear-i)==-2){
                map0.put("time",10);
                map0.put("value","10月");
            }
            if ((monthOfYear-i)==-3){
                map0.put("time",9);
                map0.put("value","9月");
            }
            if ((monthOfYear-i)>0){
                map0.put("time",monthOfYear-i);
                map0.put("value",monthOfYear-i+"月");
            }

            //封装
            list.add(map0);
        }
        //返回
        return list;
    }

    /**
     * 获取本年及前4年的日期
     * @return
     */
    public static List<Map<String, Object>> getYearDate() {
        //获取本年，及之前4年的日期
        Calendar calendar = Calendar.getInstance();
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于哪一年
        int year = calendar.get(Calendar.YEAR);
        List<Map<String,Object>> list = new ArrayList<>();
        //遍历
        for (int i = 4; i >-1 ; i--) {
            Map<String, Object> map0 = new HashMap<>();
            map0.put("time",year-i);
            map0.put("value",year-i+"年");
            //封装
            list.add(map0);
        }
        //返回
        return list;
    }


    /**
     * 根据具体年份周数获取日期范围
     * @param week
     * @return
     */
    public static String getWeekDays(int week) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //设置星期一为一周开始的第一天
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于今年的第几周
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        //获取当前年
        int yearNow = calendar.get(Calendar.YEAR);
        // 如果周大于当前时间的周，则说明是前一年的
        int year = 0;

        if (week-weekOfYear>0){
            //获取周所在的年份
            year = yearNow - 1;
        }else {
            year = yearNow;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();

        // 设置每周的开始日期(礼拜一）
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, week);
        //设置第一天
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        String beginDate = sdf.format(cal.getTime());
        //获取最后一天
        cal.add(Calendar.DAY_OF_WEEK, 6);
        String endDate = sdf.format(cal.getTime());
        //如果是本周
        int day = 0;
        if (week-weekOfYear==0){
            //当前时间是本周的第几天
            if (calendar.get(Calendar.DAY_OF_WEEK)==1){
                day = 7;
            }else {
                day = calendar.get(Calendar.DAY_OF_WEEK)-1;
            }
        }else {
            day = 7;
        }
        //返回
        return beginDate + "," + endDate+","+day;
    }

    /**
     * 根据具体年份月份获取日期范围
     * @param month
     * @return
     */
    public static String getMonthDays(int month) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //设置星期一为一周开始的第一天
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于今年的第几月
        int monthOfYear = calendar.get(Calendar.MONTH);
        //获取当前年
        int yearNow = calendar.get(Calendar.YEAR);
        // 如果周大于当前时间的周，则说明是前一年的
        int year = 0;
        if (month-monthOfYear>1){
            //获取周所在的年份
            year = yearNow - 1;
        }else {
            year = yearNow;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();

        // 设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份,cal 取出的月份比实际月份小1
        cal.set(Calendar.MONTH, month-1);
        //设置第一天
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String beginDate = sdf.format(cal.getTime());
        //获取最后一天
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.add(Calendar.DAY_OF_MONTH, lastDay-1);
        String endDate = sdf.format(cal.getTime());
        int day = 0;
        //本月
        if (month==(monthOfYear+1)) {
            //当前月第几天
            day = calendar.get(Calendar.DAY_OF_MONTH);
        }else {
            day = lastDay;
        }
        //返回
        return beginDate + "," + endDate+","+day;
    }

    /**
     *
     * @param type  type 1:周 2:月 3:年
     * @param time 时间
     * @return
     */
    public static String getDateSection(Integer type, Integer time) {
        String date = null;
        if (type==1){
            date = getWeekDays(time);
        }
        if (type==2){
            date = getMonthDays(time);
        }
        if (type==3){
            //获取当前时间
            Calendar calendar = Calendar.getInstance();
            //设置星期一为一周开始的第一天
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            //获得当前的时间戳
            calendar.setTimeInMillis(System.currentTimeMillis());
            //当前年
            if (time==calendar.get(Calendar.YEAR)) {
                //当前是今年的第几天
                int yearDay = calendar.get(Calendar.DAY_OF_YEAR);
                date = time+"-01-01"+","+time+"-12-31"+","+yearDay;
            }else {
                date = time+"-01-01"+","+time+"-12-31"+","+365;
            }

        }
        return date;
    }

    /**
     * 收入统计类型时间获取
     * @param type  type 1:月 2:季 3:年
     * @param time 时间
     * @return
     */
    public static String getDateSections(Integer type, Integer time) {
        String date = null;
        if (type==1){
            date = getMonthDays(time);
        }
        if (type==2){
            date = getQuarterDays(time);
        }
        if (type==3){
            //获取当前时间
            Calendar calendar = Calendar.getInstance();
            //设置星期一为一周开始的第一天
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            //获得当前的时间戳
            calendar.setTimeInMillis(System.currentTimeMillis());
            //当前年
            if (time==calendar.get(Calendar.YEAR)) {
                //当前是今年的第几天
                int yearDay = calendar.get(Calendar.DAY_OF_YEAR);
                date = time+"-01-01"+","+time+"-12-31"+","+yearDay;
            }else {
                date = time+"-01-01"+","+time+"-12-31"+","+365;
            }

        }
        return date;
    }

    /**
     * 获取季度时间
     * @param time
     * @return
     */
    private static String getQuarterDays(Integer time) {
        //获取当前季度上季度,前一个季度的日期
        Calendar calendar = Calendar.getInstance();
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于哪一年
        int year = calendar.get(Calendar.YEAR);
        String beginDate = "";
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if (time.equals(1)){
            beginDate = year + "-01-01";
        }
        if (time.equals(2)){
            beginDate = year + "-04-01";
        }
        if (time.equals(3)){
            beginDate = year + "-07-01";
        }
        if (time.equals(4)){
            beginDate = year + "-10-01";
        }
//        return beginDate + "," + endDate+","+day;
        return beginDate + "," + endDate;
    }

    /**
     * 获取季度时间
     * @return
     */
    public static List<Map<String, Object>> getQuarterDate() {
        //获取当前季度上季度,前一个季度的日期
        Calendar calendar = Calendar.getInstance();
        //获得当前的时间戳
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获得当前日期属于哪一年
        int year = calendar.get(Calendar.YEAR);
        //获得当前日期属于今年的第几月
        int monthOfYear = calendar.get(Calendar.MONTH)+1;
        //季度
        int quarter = 0;
        if (monthOfYear>=1 && monthOfYear<=3){
            //第一季度
            quarter = 1;
        }
        if (monthOfYear>=4 && monthOfYear<=6){
            //第一季度
            quarter = 2;
        }
        if (monthOfYear>=7 && monthOfYear<=9){
            //第一季度
            quarter = 3;
        }
        if (monthOfYear>=10 && monthOfYear<=12){
            //第一季度
            quarter = 4;
        }
        List<Map<String,Object>> list = new ArrayList<>();
        //遍历
        for (int i = 0; i >-1 ; i--) {
            Map<String, Object> map0 = new HashMap<>();
            map0.put("time",quarter);
            map0.put("value","本季度(第"+quarter+"季度)");
            //封装
            list.add(map0);
        }
        //返回
        return list;
    }
}
